package com.dd.web.base.controller;

import com.dd.common.entity.CommonResult;
import com.dd.web.base.entity.common.CommonPage;
import com.dd.web.base.entity.data.UmsAdmin;
import com.dd.web.base.entity.view.AdminUserDetails;
import com.dd.web.base.service.UmsAdminService;
import com.dd.web.base.service.UmsRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;
import java.util.HashMap;
import java.util.Map;

/**
 * 后台用户管理
 *
 */
@RestController
@Api(tags = "UmsAdminController", description = "后台用户管理")
@RequestMapping("/base/admin")
public class UmsAdminController {



    @Autowired
    private UmsAdminService adminService;
    @Autowired
    private UmsRoleService roleService;

    @ApiOperation("查询管理员列表")
    @GetMapping("/list")
    @ApiImplicitParams({
        @ApiImplicitParam(name="pageNum",value = "页码",defaultValue = "1",required = true),
        @ApiImplicitParam(name="pageSize",value = "每页行数",defaultValue = "10")
    })
    public CommonResult<CommonPage<UmsAdmin>> queryAdminPage(int pageNum,
                                                             int pageSize){
        CommonPage<UmsAdmin> page = adminService.queryAdminList(pageNum,pageSize);
        CommonResult<CommonPage<UmsAdmin>> result = CommonResult.success(page);
        return result;
    }




    @ApiOperation(value = "获取当前登录用户信息")
    @GetMapping(value = "/info")
    public CommonResult<Map<String,Object>> queryAdminInfo(Principal principal) {
        if(principal==null){
            return CommonResult.unauthorized(null);
        }

        String userName = "admin";
        AdminUserDetails umsAdmin = adminService.loadUserByUsername(userName);
        Map<String,Object> dataMap = new HashMap<>();
        dataMap.put("username",umsAdmin.getUmsAdmin().getUsername());
        dataMap.put("roles",umsAdmin.getRoleList());
        dataMap.put("icon",umsAdmin.getUmsAdmin().getIcon());
//        dataMap.put("menus", roleService.getMenuListByRoles(umsAdmin.getRoleList()));
        return CommonResult.success(dataMap);
    }


}
